<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="牌号" prop="brandNo">
      <el-input v-model="dataForm.brandNo" placeholder="牌号"></el-input>
    </el-form-item>

    <el-form-item label="牌号名称" prop="brandName">
      <el-input v-model="dataForm.brandName" placeholder="牌号名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          brandId: 0,
          brandNo: '',
          sellerId: '',
          brandName: '',
          shortCode: '',
          remark: ''
        },
        dataRule: {
          brandNo: [
            { required: true, message: '牌号不能为空', trigger: 'blur' }
          ],
          brandName: [
            { required: true, message: '牌号名称不能为空', trigger: 'blur' }
          ],
          shortCode: [
            { required: true, message: '简拼不能为空', trigger: 'blur' }
          ],
        },
				disabledSubmit:false
      }
    },
    methods: {
      init (id) {
        this.dataForm.brandId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brandId) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorebrand/info/${this.dataForm.brandId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.brandNo = data.tStoreBrand.brandNo
                this.dataForm.sellerId = data.tStoreBrand.sellerId
                this.dataForm.brandName = data.tStoreBrand.brandName
                this.dataForm.shortCode = data.tStoreBrand.shortCode
                this.dataForm.remark = data.tStoreBrand.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorebrand/${!this.dataForm.brandId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'brandId': this.dataForm.brandId || undefined,
                'brandNo': this.dataForm.brandNo,
                'sellerId': this.dataForm.sellerId,
                'brandName': this.dataForm.brandName,
                'shortCode': this.dataForm.shortCode,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
					this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.disabledSubmit = false
              }
            })
          }
          else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
